﻿using Kingdee.BOS.App;
using Kingdee.BOS.App.Core;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.CN.App.ServicePlugIn.BillConvertPlugIn;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using static Kingdee.K3.FIN.Core.Object.AP_Matck;

namespace LiMinCRMPlugin
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>
    /// 功能：
    /// 创建人：张虎
    /// 创建日期：2024/4/11 16:15:27
    /// </remarks>
    /// 
    [Description("1其他应付单-》付款单单据转换")]
    [HotUpdate]
    public class OtherPayabletoPayBill : OtherPayableToPaybillConvert
    {
        public override void OnQueryBuilderParemeter(QueryBuilderParemeterEventArgs e)
        {
            e.SelectItems.Add(new SelectorItemInfo("F_RDLV_BankAcnt")); //我方银行账户
            e.SelectItems.Add(new SelectorItemInfo("F_RDLV_PaymentPurpose"));//付款用途
            e.SelectItems.Add(new SelectorItemInfo("F_DEV_BankBranchT"));//开户银行
            e.SelectItems.Add(new SelectorItemInfo("F_DEV_BankAccountNameT"));//账户名称
            e.SelectItems.Add(new SelectorItemInfo("F_DEV_BankAccountT"));//银行账号
            e.SelectItems.Add(new SelectorItemInfo("F_DEV_BankAddress"));//银行地址
            e.SelectItems.Add(new SelectorItemInfo("F_DEV_BANKCNAPS"));//联行号
            e.SelectItems.Add(new SelectorItemInfo("FCOSTDEPARTMENTID"));//费用承担部门
            e.SelectItems.Add(new SelectorItemInfo("F_QLTK_PaymentCount"));//付款金额
        }
        /// <summary>
        /// 设置OnFieldMapping 事件启用
        /// </summary>
        /// <param name="e"></param>
        public override void OnBeforeFieldMapping(BeforeFieldMappingEventArgs e)
        {
            base.OnBeforeFieldMapping(e);
            e.FireFieldMappingEvent = true;
        }
        public object F_RDLV_BankAcnt;
        public object F_RDLV_PaymentPurpose;
        public object FSettleTypeID;
        public string F_DEV_BankBranchT;
        public string F_DEV_BankAccountNameT;
        public string F_DEV_BankAccountT;
        public string F_DEV_BankAddress;
        public string F_DEV_BANKCNAPS;
        public decimal PaymentCount;
        public object EXPENSEDEPTID;

        /// <summary>
        /// 获取与处理源单数据
        /// </summary>
        /// <param name="e"></param>
        public override void OnFieldMapping(FieldMappingEventArgs e)
        {
            List<Kingdee.BOS.Orm.DataEntity.DynamicObject> SrcData = e.ConvertSource as List<Kingdee.BOS.Orm.DataEntity.DynamicObject>;
            if (SrcData[0]["F_RDLV_BankAcnt"].ToString() != "0")
            {
                F_RDLV_BankAcnt = SrcData[0]["F_RDLV_BankAcnt"];//我方银行账号
            }
            if (SrcData[0]["F_RDLV_PaymentPurpose"].ToString() != "0")
            {
                F_RDLV_PaymentPurpose = SrcData[0]["F_RDLV_PaymentPurpose"];//付款用途
            }
            if (SrcData[0]["FCOSTDEPARTMENTID"].ToString() != "0")
            {
                EXPENSEDEPTID = SrcData[0]["FCOSTDEPARTMENTID"];//费用承担部门
            }

            F_DEV_BankBranchT = SrcData[0]["F_DEV_BankBranchT"].ToString();//开户银行
            F_DEV_BankAccountNameT = SrcData[0]["F_DEV_BankAccountNameT"].ToString();//账户名称
            F_DEV_BankAccountT = SrcData[0]["F_DEV_BankAccountT"].ToString();//银行账户
            F_DEV_BankAddress = SrcData[0]["F_DEV_BankAddress"].ToString();//银行地址
            F_DEV_BANKCNAPS = SrcData[0]["F_DEV_BANKCNAPS"].ToString();//联行号
            PaymentCount= Convert.ToDecimal(SrcData[0]["F_QLTK_PaymentCount"]);//付款金额
        }
        public override void AfterConvert(AfterConvertEventArgs e)
        {
            base.AfterConvert(e);
            ExtendedDataEntity[] heads = e.Result.FindByEntityKey("FBillHead");
            List<DynamicObject> list = new List<DynamicObject>();
            foreach (var head in heads)
            {
                DynamicObject dataEntity = head.DataEntity;
                //目标单单据体实体
                DynamicObjectCollection dycDetailEntry = head.DataEntity["PAYBILLENTRY"] as DynamicObjectCollection;


                //传金额
                dycDetailEntry[0]["PAYTOTALAMOUNTFOR"] = PaymentCount;

                //**************************付款用途*********************************
                if (F_RDLV_PaymentPurpose != null) 
                {
                    BaseDataField PurposeFld = e.TargetBusinessInfo.GetField("FPURPOSEID") as BaseDataField;
                    IViewService viewService1 = ServiceHelper.GetService<IViewService>();
                    DynamicObject[] Purpose = viewService1.LoadFromCache(this.Context,
                        new object[] { F_RDLV_PaymentPurpose },
                        PurposeFld.RefFormDynamicObjectType);
                    dycDetailEntry[0]["PURPOSEID_Id"] = F_RDLV_PaymentPurpose;
                    dycDetailEntry[0]["PURPOSEID"] = Purpose[0];
                }

                //**************************我方银行账号*********************************
                if (F_RDLV_BankAcnt !=null) 
                {
                    BaseDataField FACCOUNTID = e.TargetBusinessInfo.GetField("FACCOUNTID") as BaseDataField;
                    IViewService viewService3 = ServiceHelper.GetService<IViewService>();
                    DynamicObject[] FACCOUNT = viewService3.LoadFromCache(this.Context,
                        new object[] { F_RDLV_BankAcnt },
                        FACCOUNTID.RefFormDynamicObjectType);
                    dycDetailEntry[0]["FACCOUNTID_Id"] = F_RDLV_BankAcnt;
                    dycDetailEntry[0]["FACCOUNTID"] = FACCOUNT[0];
                }
                //**************************费用承担部门*********************************
                BaseDataField DepID = e.TargetBusinessInfo.GetField("FEXPENSEDEPTID_E") as BaseDataField;
                IViewService viewService4 = ServiceHelper.GetService<IViewService>();
                DynamicObject[] DepObjs = viewService4.LoadFromCache(this.Context,
                    new object[] { EXPENSEDEPTID },
                    DepID.RefFormDynamicObjectType);
                dycDetailEntry[0]["FEXPENSEDEPTID_E_Id"] = EXPENSEDEPTID;
                dycDetailEntry[0]["FEXPENSEDEPTID_E"] = DepObjs[0];

                //**************************费用承担部门*********************************

                dycDetailEntry[0]["OPPOSITECCOUNTNAME"] = F_DEV_BankAccountNameT;//账户名称
                dycDetailEntry[0]["OPPOSITEBANKNAME"] = F_DEV_BankBranchT;//对方开户银行
                dycDetailEntry[0]["OPPOSITEBANKACCOUNT"] = F_DEV_BankAccountT;//对方银行账号
                dycDetailEntry[0]["OpenAddressRec"] = F_DEV_BankAddress;//对方地址
                dycDetailEntry[0]["CNAPS"] = F_DEV_BANKCNAPS;//联行号

            }

        }
    }
}
